home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / ZIPPED / DOS / GRAPHICS / POVSRC.ZIP / MISC.ZIP / DAT2POV.C next >
C/C++ Source or Header  |  1992-07-03  |  7KB  |  285 lines

  1. /*
  2.  *
  3.  * cnvdat.c
  4.  * Last Update 6/10/92 - CdW
  5.  * A small program to convert DKBTrace and POV-Ray Beta V0.51 to POV-Ray V1.0
  6.  * Light sources are not converted and must be done by hand.
  7.  * Convert all uppercase keywords to lowercase.
  8.  * Converts INCLUDE to #include.
  9.  * Converts DECLARE to #declare.
  10.  * Should work as a base for more sophisticated conversions.
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. #define FALSE 0
  18. #define TRUE 1
  19. #define MAX_STRING_INDEX 41
  20. char String[MAX_STRING_INDEX];
  21. char End_Type[MAX_STRING_INDEX];
  22. int String_Index;
  23. char *start_block_words[] = {
  24.  "BICUBIC_PATCH",
  25.  "BOUNDED_BY",
  26.  "BUMPMAP",
  27.  "BUMP_MAP",
  28.  "CHECKER_TEXTURE",
  29.  "CLIPPED_BY",
  30.  "COLOR_MAP",
  31.  "COLOUR_MAP",
  32.  "COMPOSITE",
  33.  "CUBIC",
  34.  "DIFFERENCE",
  35.  "FOG",
  36.  "HEIGHT_FIELD",
  37.  "IMAGEMAP",
  38.  "IMAGE_MAP",
  39.  "INTERSECTION",
  40.  /*"LIGHT_SOURCE",*/
  41.  "MATERIAL_MAP",
  42.  "OBJECT",
  43.  "PLANE",
  44.  "POINTS",
  45.  "POLY",
  46.  "POLYGON",
  47.  "QUADRIC",
  48.  "QUARTIC",
  49.  "SHAPE",
  50.  "SMOOTH_TRIANGLE",
  51.  "SPHERE",
  52.  "TEXTURE",
  53.  "TRIANGLE",
  54.  "UNION",
  55.  "VIEW_POINT",
  56.   NULL
  57.   };
  58.  
  59. int Read_Write_Symbol (FILE *Data_File);
  60. void Begin_String();
  61. void Stuff_Character (int c);
  62. void End_String ();
  63. void Convert_String();
  64. int num_light = 0;
  65. main()
  66.  int c;  
  67.  int nest_level = 0; 
  68.  int exit = FALSE;
  69.    
  70.   fprintf(stderr,"DKBTrace\\POV-Ray Ver .5 to POV-Ray Ver 1.0 Converter\n");
  71.   fprintf(stderr,"------------------------------------------------------\n");
  72.   fprintf(stderr,"Usage: dat2pov < filename.dat > filename.pov\n");
  73.   fprintf(stderr,"Light sources must be converted by hand. See dat2pov.doc!\n");
  74.   
  75.   fprintf(stdout,"// Persistence of Vision Raytracer\n");
  76.   while(TRUE)
  77.   {
  78.     c = getc(stdin);
  79.     if(c==EOF){
  80.       fprintf(stderr,"Done Converting. End of File.\n");
  81.       break; 
  82.     }
  83.     /* Handle Comments */
  84.     if(c == '{'){
  85.       fprintf(stdout,"/*");
  86.       exit = FALSE;
  87.       while(!exit) {
  88.         c = getc(stdin);
  89.         switch(c) {
  90.           case '{':
  91.             nest_level++;
  92.             fprintf(stdout,"/*");
  93.             break;
  94.           case '}':
  95.             nest_level--;
  96.             if(nest_level == -1){
  97.               nest_level = 0;
  98.               fprintf(stdout,"*/");
  99.               exit = TRUE;
  100.             }
  101.             else
  102.               fprintf(stdout,"*/");  
  103.             break;
  104.           default:
  105.             putc(c,stdout);
  106.             break;
  107.         }  
  108.       }    
  109.      continue;
  110.     } 
  111.     
  112.     
  113.     /* KLUDGE - Skip over # since we write them later */
  114.     if(c != '#')
  115.      if(isalpha(c) || c == '_' ){
  116.       ungetc(c,stdin);
  117.       if (Read_Write_Symbol(stdin) == FALSE)
  118.         break;
  119.       }
  120.      else
  121.       putc(c,stdout);
  122.   }
  123. }   
  124.    
  125. /* Read in a symbol from the input file.  Check to see if it is a reserved
  126.    word.  If it is, write out the appropriate token.  Otherwise, write the
  127.    symbol out to the Symbol file and write out an IDENTIFIER token. An
  128.    Identifier token is a token whose token number is greater than the
  129.    highest reserved word. */
  130.  
  131. int Read_Write_Symbol (Data_File)
  132.   FILE *Data_File;
  133.   {
  134.   register int c, Symbol_Id,i;
  135.   
  136.   Begin_String();
  137.   while (TRUE)
  138.     {
  139.     c = getc(Data_File);
  140.     if (c == EOF)
  141.       {
  142.       fprintf(stderr, "Unexpected end of file");
  143.       return (FALSE);
  144.       }
  145.  
  146.     if (isalpha(c) || isdigit(c) || c == (int) '_' || c == (int) '.')
  147.       Stuff_Character (c);
  148.     else
  149.       {
  150.       ungetc (c, Data_File);
  151.       break;
  152.       }
  153.     }
  154.   End_String(Data_File);
  155.   
  156.   /* If symbol is all uppercase convert it to lower */
  157.   Convert_String();
  158.   i=0;
  159.   while(start_block_words[i]){
  160.     if(!stricmp(start_block_words[i],String)){
  161.       strcat(String," {");
  162.       break;  
  163.     }
  164.     i++;
  165.   }       
  166.   if(!stricmp(String,"colors.dat"))
  167.     strcpy(String,"colors.inc");
  168.  
  169.   if(!stricmp(String,"shapes.dat"))
  170.     strcpy(String,"shapes.inc");
  171.  
  172.   if(!stricmp(String,"shapesq.dat"))
  173.     strcpy(String,"shapesq.inc");
  174.  
  175.   if(!stricmp(String,"textures.dat"))
  176.     strcpy(String,"textures.inc");
  177.  
  178.   if(!strcmp(String,"include") || !strcmp(String,"declare")) 
  179.     putc('#',stdout);
  180.   
  181.   if(!strcmp(String,"checker_texture {") )
  182.     strcpy(String,"tiles {");    
  183.     
  184.   if(!strcmp(String,"imagemap {") )
  185.     strcpy(String,"image_map {");    
  186.   if(!strcmp(String,"bumpmap {") )
  187.     strcpy(String,"bump_map {");    
  188.   if(!strcmp(String,"phongsize") )
  189.     strcpy(String,"phong_size");    
  190.   if(!strcmp(String,"bumpsize") )
  191.     strcpy(String,"bump_size");    
  192.   if(!strcmp(String,"view_point {") )
  193.     strcpy(String,"camera {");    
  194.   
  195.    
  196.   
  197.   if(!strcmp(String,"CRed") ) 
  198.     strcpy(String,"Red");
  199.   if(!strcmp(String,"CGreen") )
  200.     strcpy(String,"Green");
  201.   if(!strcmp(String,"CBlue") )
  202.     strcpy(String,"Blue");
  203.   if(!strcmp(String,"QSphere"))
  204.     strcpy(String,"Ellipsoid");
  205.   if(!strcmp(String,"Sphere"))
  206.     strcpy(String,"Ellipsoid");
  207.   
  208.   if(!strcmp(String,"X_Disk"))
  209.     strcpy(String,"Disk_X");
  210.   if(!strcmp(String,"Y_Disk"))
  211.     strcpy(String,"Disk_Y");
  212.   if(!strcmp(String,"Z_Disk"))
  213.     strcpy(String,"Disk_Z");
  214.   
  215.   if(!strcmp(String,"Cone_X"))
  216.     strcpy(String,"QCone_X");
  217.   if(!strcmp(String,"Cone_Y"))
  218.     strcpy(String,"QCone_Y");
  219.  if(!strcmp(String,"Cone_Z"))
  220.     strcpy(String,"QCone_Z");
  221.      
  222.   
  223.   if(!strnicmp(String,"end_",4)){
  224.     strcpy(End_Type,String);
  225.     sprintf(String," }");
  226.     /* Use this line to make extra comments available */
  227.      #ifdef XTRA_COMMENTS
  228.      sprintf(String," }/* %s */",End_Type); 
  229.      #endif
  230.     }
  231.   
  232.   if(!stricmp(String,"light_source"))
  233.     fprintf(stderr,"%d Light Sources\n",++num_light);
  234.     
  235.   
  236.   fputs(String,stdout);
  237.    
  238.   return (TRUE);
  239.   }
  240.  
  241. void Begin_String()
  242.   {
  243.   String_Index = 0;
  244.   }
  245.  
  246. void Stuff_Character (c)
  247.   int c;
  248.   {
  249.   if (String_Index < MAX_STRING_INDEX)
  250.     {
  251.     String [String_Index++] = (char) c;
  252.     if (String_Index >= MAX_STRING_INDEX)
  253.       {
  254.       String [String_Index-1] = '\0';
  255.       fprintf(stderr, "String too long  %s",String);
  256.       }
  257.     }
  258.   }
  259. void End_String ()
  260.   {
  261.   Stuff_Character ((int) '\0');
  262.   }
  263.  
  264. /* If symbol is all uppercase convert it to lower */
  265. void Convert_String()
  266. {
  267.   char *tmpstr;    
  268.   
  269.   tmpstr = String;
  270.  
  271. /* check for any lower case*/ 
  272.   while(*tmpstr)
  273.     if(islower(*tmpstr++))
  274.       return;
  275. /* cnv all upper to lower */ 
  276.  tmpstr = String;
  277.   while(*tmpstr){
  278.     *tmpstr = tolower(*tmpstr);
  279.      tmpstr++;
  280.      }
  281. }
  282.  
  283.   
  284.